package cs2340.droidceptiontodo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * Category Activity Class
 * Filters by three categories
 * @author Susmita
 *
 */
public class CategoryActivity extends Activity implements RadioGroup.OnCheckedChangeListener, OnClickListener{
	RadioButton personal;
	RadioButton work;
	RadioButton school;
	RadioButton custom;
	RadioGroup categoryRG;
	//Button filtercategory;
	Button cancelcatfilter;
	EditText customText;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category);
		categoryRG = (RadioGroup)findViewById(R.id.radioGroupMain);
		//filtercategory = (Button)findViewById(R.id.filterbycategory);
		cancelcatfilter = (Button)findViewById(R.id.cancelfiltercategory);
		categoryRG.setOnCheckedChangeListener(this);
		//filtercategory.setOnClickListener(this);
		cancelcatfilter.setOnClickListener(this);
		
	}
	
	/**
     * Checks the category the user wants to filter by 
     * Once clicked, shows success.xml screen
     * 
     */
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		//Database settingsManager = new Database(this);
		Database settingsManager = Database.getInstance(this);
		settingsManager.open();
		personal = (RadioButton)findViewById(R.id.personal);
		work = (RadioButton)findViewById(R.id.work);
		school=(RadioButton)findViewById(R.id.school);
		custom=(RadioButton)findViewById(R.id.custom);
		customText=(EditText)findViewById(R.id.customText);
		if (personal.isChecked()){
			settingsManager.setSetting("filterByCategory", "Personal");
		}
		else if (work.isChecked()){
			settingsManager.setSetting("filterByCategory", "Work");
		}
		else if (school.isChecked()){
			settingsManager.setSetting("filterByCategory", "School");
		}
		settingsManager.close();
	}
	
	/**
     * Allows user to click cancel from the Category Filter screen
     * Return back to the Filter Main Screen
     * 
     */

	@Override
	public void onClick(View v) {
		if (custom.isChecked()){
			Database settingsManager = new Database(this);
			customText=(EditText)findViewById(R.id.customText);
			settingsManager.open();
			settingsManager.setSetting("filterByCategory", customText.getText().toString());
			settingsManager.close();
		}
		finish();
	} 
}
